package com.hxzy.service.system.impl;

import cn.hutool.crypto.digest.BCrypt;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hxzy.dto.system.SysUserDTO;
import com.hxzy.entity.system.SysUser;
import com.hxzy.mapper.system.SysUserMapper;
import com.hxzy.service.system.SysUserService;
import com.hxzy.vo.system.HxzyUser;
import com.hxzy.vo.system.user.UserAddDataVO;
import com.hxzy.vo.system.user.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {

    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
    public SysUser loadUserByUsername(String username) {
        LambdaQueryWrapper<SysUser> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUser::getPhone, username);
        queryWrapper.select(SysUser::getId,SysUser::getDeptId,SysUser::getPostId,SysUser::getPhone,SysUser::getPassword,SysUser::getAvatar,SysUser::getName,SysUser::getSex,SysUser::getStatus);
        return this.getOne(queryWrapper);
    }

    @Override
    public List<UserVO> selectAll(SysUserDTO userDTO) {
        return this.sysUserMapper.selectAll(userDTO);
    }

    @Override
    public Map<String, List> addData() {
        List<UserAddDataVO> deptName = this.sysUserMapper.findDeptName();
        List<UserAddDataVO> postName = this.sysUserMapper.findPostName();
        List<UserAddDataVO> roleName = this.sysUserMapper.findRoleName();
        Map<String,List> map=new HashMap<>();
        map.put("deptName",deptName);
        map.put("postName",postName);
        map.put("roleName",roleName);
        return map;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(UserVO userVO) {
        SysUser user=new SysUser();
        user.setDeptId(userVO.getDeptId());
        user.setPostId(userVO.getPostId());
        user.setName(userVO.getName());
        user.setPhone(userVO.getPhone());
        user.setSex(userVO.getSex());
        user.setStatus(userVO.getStatus());
        user.setRemark(userVO.getRemark());
        user.setCreateTime(new Date());

        // 加密
        String hashpw = BCrypt.hashpw(userVO.getPassword());
        user.setPassword(hashpw);

        // 从redis中取出当前用户信息
        HxzyUser hxzyUser= (HxzyUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String name = hxzyUser.getSysUser().getName();
        user.setCreateBy(name);

        this.sysUserMapper.addUser(user);
        Long id = user.getId();
        this.sysUserMapper.addUserRole(id,userVO.getRoleId());
    }

    @Override
    public UserVO findUser(Long id) {
        return this.sysUserMapper.findUser(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void up(UserVO userVO) {
        SysUser user=new SysUser();
        user.setDeptId(userVO.getDeptId());
        user.setPostId(userVO.getPostId());
        user.setName(userVO.getName());
        user.setPhone(userVO.getPhone());
        user.setSex(userVO.getSex());
        user.setStatus(userVO.getStatus());
        user.setRemark(userVO.getRemark());
        user.setUpdateTime(new Date());
        user.setId(userVO.getId());

        // 加密
        String hashpw = BCrypt.hashpw(userVO.getPassword());
        user.setPassword(hashpw);

        // 从redis中取出当前用户信息
        HxzyUser hxzyUser= (HxzyUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String name = hxzyUser.getSysUser().getName();
        user.setUpdateBy(name);

        this.sysUserMapper.updateById(user);
        Long id = user.getId();
        this.sysUserMapper.updateUserRole(id,userVO.getRoleId());
    }

    @Override
    public List<UserVO> findTreeUser(Long id) {
        return this.sysUserMapper.findTreeUser(id);
    }


}
